package com.laicunba.pojo.param;

import java.io.Serializable;

public class Page implements Serializable {
	private static final long serialVersionUID = -4621810942813612847L;
	public static final int DEFAULT_PAGE_SIZE = 10;
	private int pageIndex;		//当前页码
	private int pageSize;		//每页显示条数
	private int totalCount;		//总记录数
	private int pageCount;		//总页数

	public Page(int pageIndex, int pageSize) {
		if (pageIndex < 1)
			pageIndex = 1;
		if (pageSize < 1)
			pageSize = 1;
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getFirstResult() {
		return (this.pageIndex - 1) * this.pageSize;
	}

	public boolean getHasPrevious() {
		return this.pageIndex > 1;
	}

	public boolean getHasNext() {
		return this.pageIndex < this.pageCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		this.pageCount = (totalCount / this.pageSize + (totalCount % this.pageSize != 0 ? 1 : 0));
		if (totalCount == 0) {
			if (this.pageIndex != 1)
				throw new IndexOutOfBoundsException("Page index out of range.");
		}
		else if (this.pageIndex > this.pageCount)
			throw new IndexOutOfBoundsException("Page index out of range.");
	}

	public boolean isEmpty() {
		return this.totalCount == 0;
	}
}
